// pages/account/namecheck/certifydetail.js
var common = require('../../../common.js')
Page({
  data: {
    provinces: [],
    province: "",
    citys: [],
    city: "",
    countys: [],
    county: '',
    contact_address:'',
    value: [0, 0, 0],
    values: [0, 0, 0],
    condition: false
  },
  onLoad: function () {
    var that = this;
    wx.getStorage({
      key: 'user',
      success: function (res) {
        common.cRequst({
          url: 'http://' + common.serveIp + '/user.php?c=dp_authentication&a=approveInfo',
          data: {
            "session_id": res.data.session_id,
            "type": "3",
            "version": "app"
          },
          method: "Post",
          success: function (data) {
            var obj = {};
            obj = data.data[0];
            console.log(obj);
            that.setData(obj)
          }
        })
      }
    })
    common.cRequst({
      url: 'http://' + common.serveIp + '/user.php?c=dp_city&a=city',
      success: function (res) {
        console.log(res);
        that.setData({
          cityData: JSON.parse(res.data),
        })
        var cityData = that.data.cityData;
        const provinces = [];
        const citys = [];
        const countys = [];
        for (let i = 0; i < cityData.length; i++) {
          provinces.push(cityData[i].name);
        }
        for (let i = 0; i < cityData[0].sub.length; i++) {
          citys.push(cityData[0].sub[i].name)
        }
        for (let i = 0; i < cityData[0].sub[0].sub.length; i++) {
          countys.push(cityData[0].sub[0].sub[i].name)
        }
        that.setData({
          'provinces': provinces,
          'citys': citys,
          'countys': countys,
          'province': cityData[0].name,
          'contact_address': cityData[0].name + " " + cityData[0].sub[0].name + " " + cityData[0].sub[0].sub[0].name,
          'city': cityData[0].sub[0].name,
          'county': cityData[0].sub[0].sub[0].name
        })
      }
    });
  },
  upimg: function (e) {
    var target = e.currentTarget;
    var dataset = target.dataset;
    var that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        var tempFilePaths = res.tempFilePaths
        var obj = {};
        var filetype = res.tempFilePaths[0].substring(res.tempFilePaths[0].lastIndexOf('.'), res.tempFilePaths[0].length)
        wx.getStorage({
          key: 'user',
          success: function (data) {
            // success
            console.log(data)
            common.cRequst({
              url: 'http://' + common.serveIp + '/user.php?c=dp_entrust&a=uploadTransfer',
              data: {
                "session_id": data.data.session_id,
                "version": "app"
              },
              method: "Post",
              header: {
                'content-type': 'application/json'
              },
              success: function (d) {
                console.log(d);
                wx.uploadFile({
                  url: d.data.host, //仅为示例，非真实的接口地址
                  filePath: tempFilePaths[0],
                  name: 'file',
                  formData: {
                    "OSSAccessKeyId": d.data.accessid,
                    "policy": d.data.policy,
                    "signature": d.data.signature,
                    "key": d.data.dir + d.data.name + filetype,
                    "success_action_status": 200
                  },
                  success: function (b) {
                    var data = b.data;
                    //do something
                    var img = d.data.host + '/' + d.data.dir + d.data.name + filetype;
                    console.log(img)
                    obj[dataset['name']] = img
                    that.setData(obj)
                    wx.showToast({
                      title: '上传成功',
                      icon: 'success',
                      duration: 0
                    })
                  },
                  fail: function (res) {
                    wx.showToast({
                      title: '上传失败',
                      duration: 0
                    })
                  }
                })
              }
            })
          }
        })
      }
    })
  },
  formSubmit: function (e) {
    var that = this;
    var regName = /^[\u4E00-\u9FA5]{2,4}$/;
    var regIdcard = /^\d{6}(18|19|20)?\d{2}(0[1-9]|1[12])(0[1-9]|[12]\d|3[01])\d{3}(\d|X)$/i;
    var regTel = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/;
    var regEmail = /^(\w-*\.*)+@(\w-?)+(\.\w{2,})+$/;
    e.detail.value.idcard_front = this.data.imgurl1||this.data.idcard_front;
    e.detail.value.idcard_back = this.data.imgurl2||this.data.idcard_back;
    e.detail.value.idcard_img = this.data.imgurl3||this.data.idcard_img;
    e.detail.value.contact_address = this.data.province + " " + this.data.city + " " + this.data.county;
    console.log(e.detail.value);
    if (!regName.test(e.detail.value.contact_name)) {
      wx.showToast({
        title: '姓名格式错误',
      })
    }
    else if (!regIdcard.test(e.detail.value.idcard)) {
      wx.showToast({
        title: '身份证格式错误',
      })
    }
    else if (e.detail.value.idcard_front == undefined) {
      wx.showToast({
        title: '请上传身份证正面照',
      })
    }
    else if (e.detail.value.idcard_back == undefined) {
      wx.showToast({
        title: '请上传身份证反面照',
      })
    }
    else if (e.detail.value.idcard_img == undefined) {
      wx.showToast({
        title: '请上传手持身份证照照片',
      })
    }
    else if (!regTel.test(e.detail.value.contact_tel)) {
      wx.showToast({
        title: '手机号格式错误',
      })
    }
    else if (e.detail.value.email !== "" && !regEmail.test(e.detail.value.email)) {
      wx.showToast({
        title: '邮箱格式错误',
      })
    }
    else if (e.detail.value.contact_address == "") {
      wx.showToast({
        title: '未选择经营地址',
      })
    }
    else if (e.detail.value.contact_address_info == "") {
      wx.showToast({
        title: '未填写详细地址',
      })
    }
    else {
      wx.getStorage({
        key: 'user',
        success: function (res) {
          common.cRequst({
            url: 'http://' + common.serveIp + '/user.php?c=dp_authentication&a=approve',
            data: {
              'type': 1,
              'session_id': res.data.session_id,
              'version': "app",
              'contact_address': that.data.contact_address,
              'contact_name': e.detail.value.contact_name,
              'contact_tel': e.detail.value.contact_tel,
              'email': e.detail.value.email,
              'idcard': e.detail.value.idcard,
              'idcard_back': e.detail.value.idcard_back,
              'idcard_front': e.detail.value.idcard_front,
              'idcard_img': e.detail.value.idcard_img,
              'contact_address_info': e.detail.value.contact_address_info,
              'id':that.data.id
            },
            method: "Post",
            success:function(res){
              wx.showToast({
                title:'提交成功'
              })
              wx.redirectTo({
                url: "/pages/account/namecheck/namecheck"
              })
            }
          })
        },
      })

    }
    e.detail.value
  },
  bindChange: function (e) {
    //console.log(e);
    var val = e.detail.value
    var t = this.data.values;
    var cityData = this.data.cityData;

    if (val[0] != t[0]) {
      console.log('province no ');
      const citys = [];
      const countys = [];

      for (let i = 0; i < cityData[val[0]].sub.length; i++) {
        citys.push(cityData[val[0]].sub[i].name)
      }
      for (let i = 0; i < cityData[val[0]].sub[0].sub.length; i++) {
        countys.push(cityData[val[0]].sub[0].sub[i].name)
      }

      this.setData({
        province: this.data.provinces[val[0]],
        city: cityData[val[0]].sub[0].name,
        citys: citys,
        county: cityData[val[0]].sub[0].sub[0].name,
        countys: countys,
        values: val,
        value: [val[0], 0, 0],
        contact_address: this.data.provinces[val[0]] + " " + cityData[val[0]].sub[0].name + " " + cityData[val[0]].sub[0].sub[0].name,
      })

      return;
    }
    if (val[1] != t[1]) {
      console.log('city no');
      const countys = [];

      for (let i = 0; i < cityData[val[0]].sub[val[1]].sub.length; i++) {
        countys.push(cityData[val[0]].sub[val[1]].sub[i].name)
      }

      this.setData({
        city: this.data.citys[val[1]],
        county: cityData[val[0]].sub[val[1]].sub[0].name,
        countys: countys,
        values: val,
        value: [val[0], val[1], 0],
        contact_address: cityData[val[0]].name + " " + this.data.citys[val[1]] + " " + cityData[val[0]].sub[val[1]].sub[0].name,
      })
      return;
    }
    if (val[2] != t[2]) {
      console.log('county no');
      this.setData({
        county: this.data.countys[val[2]],
        values: val,
        contact_address: cityData[val[0]].name + " " + cityData[val[0]].sub[val[1]].name + " " + this.data.countys[val[2]],
      })
      return;
    }


  },
  open: function () {
    this.setData({
      condition: !this.data.condition
    })
  },
})